//
//  HMMainTabBar.swift
//  HMWeibo1
//
//  Created by liGordon on 16/4/9.
//  Copyright © 2016年 lifuli. All rights reserved.
//

import UIKit

class HMMainTabBar: UITabBar {


// 1. 定义闭包属性,传递
    var composeClosure: (() -> ())?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        

        
        let width = self.frame.size.width / CGFloat(HMMainTabBarButtonCount)
        
        let newFrame = CGRect(x: 0, y: 0, width: width, height: frame.size.height)
        
        var index = 0
        
        for subView in  subviews {
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                subView.frame = CGRectOffset(newFrame, CGFloat(index) * width, 0)
                
                index += index == 1 ? 2 : 1
                
            }
        }
      composeButton.frame = CGRect(x:  2 * width, y: 0, width: width, height: frame.size.height)
        
    }
    //Mark - 按钮点击事件
    @objc private func didClickComposeButton() {
        // 调用闭包
        composeClosure?()
    }
    
    

    // Mark - 懒加载
    private lazy var composeButton: UIButton = {
       
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: Selector("didClickComposeButton"), forControlEvents: UIControlEvents.TouchUpInside)
        
        self.addSubview(button)
        
        return button
        
    }()
    
}
